package cn.jinjimi.app.custom.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import cn.jinjimi.base.util.AndroidUtils;
import cn.jinjimi.app.R;
import cn.jinjimi.app.custom.entity.Custom;
import cn.jinjimi.app.custom.utils.CustomContactHelper;
import cn.jinjimi.app.store.entity.custom.ContactInfo;
import cn.jinjimi.app.main.adapter.extend.MyBaseAdapter;

/**
 * Created by libilang on 15/11/7.
 */
public class PhonesSelectDialog extends Dialog implements AdapterView.OnItemClickListener {

    private Context mContext;
    private Custom mCustom;
    private int actionType;

    private ListView mListView;
    private PhonesSelectAdapter mAdapter;

    public PhonesSelectDialog(Context context, Custom custom, int type) {
        super(context);
        mContext = context;
        mCustom = custom;
        actionType = type;

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_phones_select);

        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setGravity(Gravity.CENTER);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        setCanceledOnTouchOutside(true);

        initView();
    }

    private void initView() {
        mAdapter = new PhonesSelectAdapter(mContext);
        mListView = (ListView) findViewById(R.id.phone_listview);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(this);

        mAdapter.setItems(mCustom.getContactInfos());
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        ContactInfo contactInfo = mAdapter.getItem(position - mListView.getHeaderViewsCount());
        switch (actionType) {
            case CustomContactHelper.TypeCall:
                AndroidUtils.call(mContext, contactInfo.getValue());
                break;
            case CustomContactHelper.TypeMsg:
                AndroidUtils.sendMsg(mContext, contactInfo.getValue(), "");
                break;
        }
        dismiss();
    }

    public class PhonesSelectAdapter extends MyBaseAdapter<ContactInfo> {

        public PhonesSelectAdapter(Context context) {
            mContext = context;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            PhonesHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_phones, null);
                holder = new PhonesHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (PhonesHolder) convertView.getTag();
            }

            ContactInfo item = getItem(position);

            holder.phone.setText(item.getValue());
            return convertView;
        }

        class PhonesHolder {
            public TextView phone;

            public PhonesHolder(View view) {
                phone = (TextView) view.findViewById(R.id.check_tag);
            }
        }
    }
}